package com.dodoke.common.utils;


import com.dodoke.common.exception.NoSuchRandomException;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomUtil {

    /***
     * 获取随机不重复的数字
     * @param count 随机次数
     * @param bound 随机的范围
     * @return 将每次随机的数字放到一个数组中，然后返回
     */
    public static List<Integer> getNoRepeatRamdom(int count, int bound) {
        if (count <= 0) {
            return null;
        }
        if (Math.abs(bound) < count) {
            throw new NoSuchRandomException("随机的范围不能小于随机的次数");
        }
        Random random = new Random();
        List<Integer> list = new ArrayList<>(10);
        for (int i = 0; i < count; i++) {
            int num = random.nextInt(bound);
            if (list.contains(num)) {
                i --;
            }else {
                list.add(num);
            }
        }
        return list;
    }
}
